package model

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
	"starry_sky/internal/core"
	"strings"
	"time"
)

// BaseID 基础id
type BaseID struct {
	ID string `json:"id" gorm:"type:varchar(32);primary_key" form:"id"`
}

// BeforeCreate 钩子用于在创建记录前生成 UUID
func (b *BaseID) BeforeCreate(tx *gorm.DB) error {
	if b.ID == core.EmptyID {
		b.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
	}
	return nil
}

// BaseTime 创建时间
type BaseTime struct {
	CreatedAt time.Time
	UpdatedAt time.Time
}

// BaseDeleteTime 删除时间
type BaseDeleteTime struct {
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

// BaseModel 全功能model
type BaseModel struct {
	BaseID
	BaseTime
	BaseDeleteTime
}

// BaseModelWithoutDelete 不包含deleteAt的model
type BaseModelWithoutDelete struct {
	BaseID
	BaseTime
}

func NewBaseModelById(id string) *BaseModel {
	return &BaseModel{
		BaseID: BaseID{
			ID: id,
		},
	}
}
